#include "builder.hh"
#include "tree.hh"
#include "cppn/cppn.hh"
#include "cppn/constants.hh"
#include "visualize.hh"
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
using namespace brain;

int main(int argc, char** argv) {

    if (argc == 2) {
        ifstream fin(argv[1]);
        shared_ptr<evolution::Genome> genome = make_shared<evolution::Genome>(cppn::TOTAL_INPUT_NODES);
        fin >> *genome;

        cppn::CPPN cppn{genome};
        Builder builder{&cppn};

        vector<pair<float, float>> inputs = {
         //  {0.5f, 0.0f},   
        };
        vector<pair<float, float>> outputs = {
          //  {0.5f, 0.5f},// {0.25f, 0.25f}
        };
        for (float a = -9; a <= 9; ++a) {
            for (float b = 6; b <= 6; ++b) {
                float x = a/15.0f;
                float y = b/10.0f;

                outputs.push_back(make_pair(x, y));
                inputs.push_back(make_pair(x, -y));
            }
        }

        builder.add_inputs(inputs);
        builder.add_outputs(outputs);
        builder.construct();

        bool exit = false;
        while (!exit) {
            NetworkWindow visualizer { 600, 600, &builder };
            visualizer.show();
            visualizer.render();

            bool quit = false;
            SDL_Event e;
            while (!quit) {
                
                while (SDL_PollEvent(&e)) {
                    if (e.type == SDL_QUIT 
                    || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_q)) { exit = true; quit = true; }
                    else if (e.type == SDL_MOUSEBUTTONDOWN) {
                        if (e.button.button == SDL_BUTTON_LEFT) {
                            visualizer.click(e.button.x, e.button.y, true);
                        } else {
                            visualizer.click(e.button.x, e.button.y, false);
                        }
                    }
                }
                visualizer.wait(10);
            }
        }
    }
}




























